package cn.edu.scau.cmi.ema.dao;

import cn.edu.scau.cmi.ema.base.CmiPagination;
import cn.edu.scau.cmi.ema.domain.Feature;
import cn.edu.scau.cmi.ema.util.CmiSqlUtil;
import org.hibernate.Session;
import org.hibernate.criterion.DetachedCriteria;
import org.springframework.dao.DataAccessException;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import javax.persistence.EntityManager;
import javax.persistence.NoResultException;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import java.util.Arrays;
import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.Set;

@Repository("FeatureDAO")
@Transactional
public class FeatureDAOImpl extends AbstractJpaDao<Feature> implements FeatureDAO {

	private final static Set<Class<?>> dataTypes = new HashSet<Class<?>>(
			Arrays.asList(new Class<?>[] { Feature.class }));

	@PersistenceContext()
	private EntityManager entityManager;

	public FeatureDAOImpl() {
		super();
	}

	public EntityManager getEntityManager() {
		return entityManager;
	}

	public Set<Class<?>> getTypes() {
		return dataTypes;
	}

	@Transactional
	public Feature findFeatureById(Integer id) throws DataAccessException {

		return findFeatureById(id, -1, -1);
	}

	@Transactional
	public Feature findFeatureById(Integer id, int startResult, int maxRows) throws DataAccessException {
		try {
			Query query = createNamedQuery("findFeatureById", startResult, maxRows, id);
			return (Feature) query.getSingleResult();
		} catch (NoResultException nre) {
			return null;
		}
	}

	@Transactional
	public Set<Feature> findFeatureByName(String name) throws DataAccessException {

		return findFeatureByName(name, -1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Feature> findFeatureByName(String name, int startResult, int maxRows) throws DataAccessException {
		Query query = createNamedQuery("findFeatureByName", startResult, maxRows, name);
		return new LinkedHashSet<Feature>(query.getResultList());
	}

	@Transactional
	public Set<Feature> findFeatureByNameContaining(String name) throws DataAccessException {

		return findFeatureByNameContaining(name, -1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Feature> findFeatureByNameContaining(String name, int startResult, int maxRows)
			throws DataAccessException {
		Query query = createNamedQuery("findFeatureByNameContaining", startResult, maxRows, name);
		return new LinkedHashSet<Feature>(query.getResultList());
	}

	@Transactional
	public Set<Feature> findFeatureByDescription(String description) throws DataAccessException {

		return findFeatureByDescription(description, -1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Feature> findFeatureByDescription(String description, int startResult, int maxRows)
			throws DataAccessException {
		Query query = createNamedQuery("findFeatureByDescription", startResult, maxRows, description);
		return new LinkedHashSet<Feature>(query.getResultList());
	}

	@Transactional
	public Set<Feature> findFeatureByDescriptionContaining(String description) throws DataAccessException {

		return findFeatureByDescriptionContaining(description, -1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Feature> findFeatureByDescriptionContaining(String description, int startResult, int maxRows)
			throws DataAccessException {
		Query query = createNamedQuery("findFeatureByDescriptionContaining", startResult, maxRows, description);
		return new LinkedHashSet<Feature>(query.getResultList());
	}

	@Transactional
	public Feature findFeatureByPrimaryKey(Integer id) throws DataAccessException {
		return findFeatureByPrimaryKey(id, -1, -1);
	}

	@Transactional
	public Feature findFeatureByPrimaryKey(Integer id, int startResult, int maxRows) throws DataAccessException {
		try {
			Query query = createNamedQuery("findFeatureByPrimaryKey", startResult, maxRows, id);
			return (Feature) query.getSingleResult();
		} catch (NoResultException nre) {
			return null;
		}
	}

	@Transactional
	public Set<Feature> findFeaturesByIds(Set<Integer> ids) throws DataAccessException {
		try {
			Query query = createNamedQuery("findFeaturesByIds", -1, -1, ids);
			return new HashSet(query.getResultList());
		} catch (NoResultException nre) {
			return null;
		}
	}

	@Transactional
	public Set<Feature> findAllFeatures() throws DataAccessException {

		return findAllFeatures(-1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Feature> findAllFeatures(int startResult, int maxRows) throws DataAccessException {
		Query query = createNamedQuery("findAllFeatures", startResult, maxRows);
		return new LinkedHashSet<Feature>(query.getResultList());
	}

	@SuppressWarnings("unchecked")
	public Set<Feature> findFeaturesByDetectmethod(Integer detectmethodId, int start, int max)
			throws DataAccessException {
		Query query = createNamedQuery("findAllFeaturesByDetectmethod", start, max, detectmethodId);
		return new LinkedHashSet<Feature>(query.getResultList());
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Feature> findFeatureByCompositeProperities(String sql, int startResult, int maxRows) {
		return new LinkedHashSet<Feature>(createQuery(sql, startResult, maxRows).getResultList());
	}

	@Transactional
	public Set<Feature> findFeaturesByDetachedCriteria(CmiPagination page) throws DataAccessException {
		DetachedCriteria detachedCriteria = CmiSqlUtil.createDetachedCriteria(page, Feature.class);
		Session session = (Session) entityManager.getDelegate();
		Set<Feature> features = new HashSet(detachedCriteria.getExecutableCriteria(session)
				.setFirstResult(page.getSearchBeginCursor()).setMaxResults(page.getPaginationSize()).list());

		return features;
	}

	@Transactional
	public Feature findFeatureByPhysicalPrimaryKey(String name, Integer detectmethodId) throws DataAccessException {
		return findFeatureByPhysicalPrimaryKey(name, detectmethodId, -1, -1);
	}

	@Transactional
	public Feature findFeatureByPhysicalPrimaryKey(String name, Integer detectmethodId, int startResult, int maxRows)
			throws DataAccessException {
		try {
			Query query = createNamedQuery("findFeatureByPhyscialPrimaryKey", startResult, maxRows, name,
					detectmethodId);
			return (Feature) query.getSingleResult();
		} catch (NoResultException nre) {
			return null;
		}
	}

	public boolean canBeMerged(Feature entity) {
		return true;
	}
}
